package org.amphiprion.descentv2assistant.dao.db;

import java.util.ArrayList;
import java.util.List;

import org.amphiprion.descentv2assistant.entity.EncounterMonster;
import org.amphiprion.descentv2assistant.entity.GameSession;

import android.content.Context;
import android.database.Cursor;

public class EncounterMonsterDao extends AbstractDbDao {
	private static EncounterMonsterDao instance;

	/**
	 * Hidden constructor.
	 * 
	 * @param context
	 *            the application context
	 */
	private EncounterMonsterDao(Context context) {
		super(context);
	}

	/**
	 * Return the singleton.
	 * 
	 * @param context
	 *            the application context
	 * @return the singleton
	 */
	public static EncounterMonsterDao getInstance(Context context) {
		if (instance == null) {
			instance = new EncounterMonsterDao(context);
		}
		return instance;
	}

	public void persist(EncounterMonster entity) {
		if (isInDb(entity.getId())) {
			update(entity);
		} else {
			create(entity);
		}
	}

	private void create(EncounterMonster entity) {
		String sql = "insert into encounter_monster (" + "id, game_session_id, tag, mob_id, mob_type, health, condition, act" + ") values (?,?,?,?,?,?,?,?)";

		Object[] params = new Object[8];
		params[0] = entity.getId();
		params[1] = entity.getGameId();
		params[2] = entity.getTag();
		params[3] = entity.getMonster().getId();
		params[4] = entity.getMonster().getClass().getSimpleName();
		params[5] = entity.getHealth();
		params[6] = entity.getConditionsForDB();
		params[7] = entity.getAct();

		execSQL(sql, params);

	}

	private void update(EncounterMonster entity) {
		String sql = "update encounter_monster set game_session_id=?, tag=?, mob_id=?, mob_type=?, health=?, condition=?, act=? where id=?";

		Object[] params = new Object[8];
		params[0] = entity.getGameId();
		params[1] = entity.getTag();
		params[2] = entity.getMonster().getId();
		params[3] = entity.getMonster().getClass().getSimpleName();
		params[4] = entity.getHealth();
		params[5] = entity.getConditionsForDB();
		params[6] = entity.getAct();
		params[7] = entity.getId();

		execSQL(sql, params);

	}

	public boolean isInDb(String id) {
		boolean result = false;
		String sql = "SELECT count(*) from encounter_monster where id='" + id + "'";
		Cursor cursor = getDatabase().rawQuery(sql, null);
		cursor.moveToFirst();
		result = cursor.getInt(0) != 0;
		cursor.close();
		return result;
	}

	public List<EncounterMonster> getAll(GameSession gameSession) {
		String sql = "select " + "id, game_session_id, tag, mob_id, mob_type, health, condition, act from encounter_monster where game_session_id='" + gameSession.getId() + "'";
		Cursor cursor = getDatabase().rawQuery(sql, null);
		ArrayList<EncounterMonster> result = new ArrayList<EncounterMonster>();

		if (cursor.moveToFirst()) {
			do {
				EncounterMonster entity = new EncounterMonster(cursor);
				result.add(entity);
			} while (cursor.moveToNext());
		}
		cursor.close();
		return result;
	}

	public void delete(EncounterMonster entity) {
		String sql = "delete from encounter_monster where id=?";

		Object[] params = new Object[1];
		params[0] = entity.getId();

		execSQL(sql, params);

	}

}
